package com.yimi.yinhepda.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.pda.uce.commom.util.LogUtil;
import com.yancy.gallerypick.config.GalleryConfig;
import com.yancy.gallerypick.config.GalleryPick;
import com.yancy.gallerypick.inter.IHandlerCallBack;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.check_balance.GlideSimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GalleryPickView extends LinearLayout implements View.OnClickListener {

    /**
     * 修复前和修复后图片view
     */
    private SmartHeightGridView mGridView;
    private SmartHeightGridView mGridView2;
    /**
     * 图片上传view
     */
    private SmartHeightGridView mTakePhotoGridView;
    /**
     * 图片选择器
     */
    private GalleryConfig galleryConfig;
    /**
     * 选择图片路径
     */
    private ArrayList<String> pickPic = new ArrayList<>();
    /**
     * 照片上传路径
     */
    private ArrayList<String> pickPicTakePhoto = new ArrayList<>();
    /**
     * 修复前图片路径
     */
    private ArrayList<String> pickPicBefor = new ArrayList<>();
    /**
     * 修复后图片路径
     */
    private ArrayList<String> pickPicAfter = new ArrayList<>();
    /**
     * 照片上传展示器
     */
    private GlideSimpleAdapter takePhotoAdapter;
    /**
     * 修复前图片展示器
     */
    private GlideSimpleAdapter simpleAdapter;
    /**
     * 修复后图片展示器
     */
    private GlideSimpleAdapter simpleAdapter2;
    /**
     * 修复前存储图片集合
     */
    private List<HashMap<String, Object>> dataList0 = new ArrayList<>();
    /**
     * 修复前存储图片集合
     */
    private List<HashMap<String, Object>> dataList = new ArrayList<>();
    /**
     * 修复后存储图片集合
     */
    private List<HashMap<String, Object>> dataList2 = new ArrayList<>();

    private ImageView ivPicTake0;
    private ImageView ivPicTake;
    private ImageView ivPicTake2;
    /**
     * 1-修复前照片，2-修复后照片，3-照片上传
     */
    private int picType;
    /**
     * 照片上传布局
     */
    private LinearLayout llPicTakePhtot;
    /**
     * 修复前和修复后照片布局
     */
    private LinearLayout llPicRepairPhoto;
    /**
     * 修复后照片上传  线性布局
     */
    private LinearLayout llIsRepairAfter;
    private RelativeLayout rlImg0;
    private RelativeLayout rlgv0;
    private RelativeLayout rlImg;
    private RelativeLayout rlgv;
    private RelativeLayout rlImg2;
    private RelativeLayout rlgv2;
    private Context mContext;
    private final int PERMISSIONS_REQUEST_READ_CONTACTS = 101;
    private int PICURE_NUM = 8;
    boolean isGalleryConfigPick = false;
    /**
     * 修复前照片
     */
    boolean canTakePhoto1 = false;
    /**
     * 修复后照片
     */
    boolean canTakePhoto2 = false;
    /**
     * 照片上传
     */
    boolean canTakePhoto3 = false;
    public static final String[] PERMISSIONS = new String[]{Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    public static final int REQUEST_CODE_PERMISSIONS = 101;

    public GalleryPickView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_gallery_pick, this);
        llPicTakePhtot = findViewById(R.id.ll_pic_take_phtot);
        mTakePhotoGridView = findViewById(R.id.gv_notitle0);
        ivPicTake0 = findViewById(R.id.iv_pic_take0);
        llPicRepairPhoto = findViewById(R.id.ll_pic_repair_phtot);
        mGridView = findViewById(R.id.gv_notitle);
        mGridView2 = findViewById(R.id.gv_notitle2);
        ivPicTake = findViewById(R.id.iv_pic_take);
        ivPicTake2 = findViewById(R.id.iv_pic_take2);
        rlImg0 = findViewById(R.id.rl_img0);
        rlgv0 = findViewById(R.id.rl_gv0);
        rlImg = findViewById(R.id.rl_img);
        rlgv = findViewById(R.id.rl_gv);
        rlImg2 = findViewById(R.id.rl_img2);
        rlgv2 = findViewById(R.id.rl_gv2);
        llIsRepairAfter = findViewById(R.id.ll_isRepairAfter);

        pickPic = new ArrayList<>();
        pickPicTakePhoto = new ArrayList<>();
        pickPicBefor = new ArrayList<>();
        pickPicAfter = new ArrayList<>();
        dataList0 = new ArrayList<>();
        dataList = new ArrayList<>();
        dataList2 = new ArrayList<>();

        takePhotoAdapter = new GlideSimpleAdapter(context, dataList0, R.layout.gridview_item, new String[]{"itemImage"},
                new int[]{R.id.imageView1}, pickPicTakePhoto);
        mTakePhotoGridView.setAdapter(takePhotoAdapter);

        simpleAdapter = new GlideSimpleAdapter(context, dataList, R.layout.gridview_item, new String[]{"itemImage"},
                new int[]{R.id.imageView1}, pickPicBefor);
        mGridView.setAdapter(simpleAdapter);

        simpleAdapter2 = new GlideSimpleAdapter(context, dataList2, R.layout.gridview_item, new String[]{"itemImage"},
                new int[]{R.id.imageView1}, pickPicAfter);
        mGridView2.setAdapter(simpleAdapter2);

        //初始化图片选择框架
        galleryConfig = new GalleryConfig.Builder()
                // 监听接口（必填）
                .iHandlerCallBack(iHandlerCallBack)
                // provider (必填)
                .imageLoader(new GlideImageLoader()).provider("com.yimi.yinhepda.fileprovider")
                // 记录已选的图片
                .pathList(pickPic)
                // 配置是否多选的同时 配置多选数量   默认：false
                .multiSelect(true, PICURE_NUM)
                // 快捷开启裁剪功能，仅当单选 或直接开启相机时有效
                .crop(false)
                // 配置裁剪功能的参数，   默认裁剪比例 1:1
                .crop(false, 1, 1, 500, 500)
                // 是否实现相机按钮  默认：false
                .isShowCamera(true)
                // 图片存放路径
                .filePath("/Gallery/Pictures").build();

        ivPicTake.setOnClickListener(this);
        ivPicTake2.setOnClickListener(this);
    }

    public GalleryPickView(final Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    /**
     * 图片加载框架，返回图接口
     */
    IHandlerCallBack iHandlerCallBack = new IHandlerCallBack() {
        @Override
        public void onStart() {
            LogUtil.e("onStart: 开启");
        }

        @Override
        public void onSuccess(List<String> photoList) {
            resetPicture(photoList);
        }

        @Override
        public void onCancel() {
            LogUtil.e("onCancel: 取消");
        }

        @Override
        public void onFinish() {
            LogUtil.e("onFinish: 结束");
        }

        @Override
        public void onError() {
            LogUtil.e("onError: 出错");
        }
    };

    public void resetPicture(List<String> photoList) {
        pickPic.clear();
        if (photoList != null && photoList.size() > 0) {
            pickPic.addAll(photoList);
        }
        if (picType == 1) {
            //修复前照片
            rlgv.setVisibility(View.VISIBLE);
            rlImg.setVisibility(View.GONE);
            dataList.clear();
            pickPicBefor.clear();
            pickPicBefor.addAll(pickPic);
            if (photoList != null) {
                for (int i = 0; i < photoList.size(); i++) {
                    HashMap<String, Object> gridviewItem = new HashMap<>();
                    gridviewItem.put("itemImage", photoList.get(i));
                    dataList.add(gridviewItem);
                }
            }
            if (photoList.size() != PICURE_NUM) {
                HashMap<String, Object> gridviewItem = new HashMap<>();
                gridviewItem.put("itemImage", R.mipmap.ic_camera);
                dataList.add(gridviewItem);
                canTakePhoto1 = true;
            } else {
                canTakePhoto1 = false;
            }
            simpleAdapter.notifyDataSetChanged();

        } else if (picType == 2) {
            //修复后照片
            rlgv2.setVisibility(View.VISIBLE);
            rlImg2.setVisibility(View.GONE);
            dataList2.clear();
            pickPicAfter.clear();
            pickPicAfter.addAll(pickPic);
            if (photoList != null) {
                for (int i = 0; i < photoList.size(); i++) {
                    HashMap<String, Object> gridviewItem = new HashMap<>();
                    gridviewItem.put("itemImage", photoList.get(i));
                    dataList2.add(gridviewItem);
                }
            }
            if (photoList.size() != PICURE_NUM) {
                HashMap<String, Object> gridviewItem = new HashMap<>();
                gridviewItem.put("itemImage", R.mipmap.ic_camera);
                dataList2.add(gridviewItem);
                canTakePhoto2 = true;
            } else {
                canTakePhoto2 = false;
            }
            simpleAdapter2.notifyDataSetChanged();
        } else if (picType == 3) {
            //照片上传
            rlgv0.setVisibility(View.VISIBLE);
            rlImg0.setVisibility(View.GONE);
            dataList0.clear();
            pickPicTakePhoto.clear();
            pickPicTakePhoto.addAll(pickPic);
            if (photoList != null) {
                for (int i = 0; i < photoList.size(); i++) {
                    HashMap<String, Object> gridviewItem = new HashMap<>();
                    gridviewItem.put("itemImage", photoList.get(i));
                    dataList0.add(gridviewItem);
                }
            }
            if (photoList.size() != PICURE_NUM) {
                HashMap<String, Object> gridviewItem = new HashMap<>();
                gridviewItem.put("itemImage", R.mipmap.ic_camera);
                dataList0.add(gridviewItem);
                canTakePhoto3 = true;
            } else {
                canTakePhoto3 = false;
            }
            takePhotoAdapter.notifyDataSetChanged();
        }

        if (!isGalleryConfigPick) {
            galleryConfig.getBuilder().pathList(pickPic);
        }
        isGalleryConfigPick = false;
    }

    /**
     * 拍照选择图片功能
     */
    public void takePhotoFunction(final Context context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionUtils.checkMorePermissions(context, PERMISSIONS, new PermissionUtils.PermissionCheckCallBack() {
                @Override
                public void onHasPermission() {
                    takePhoto();
                }

                @Override
                public void onUserHasAlreadyTurnedDown(String... permission) {
                    PermissionUtils.requestMorePermissions(context, PERMISSIONS, REQUEST_CODE_PERMISSIONS);
                }

                @Override
                public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
                    PermissionUtils.requestMorePermissions(context, PERMISSIONS, REQUEST_CODE_PERMISSIONS);
                }
            });
        } else {
            takePhoto();
        }
    }

    private void takePhoto() {
        pickPic.clear();
        if (picType == 1) {
            //修复前照片
            pickPic.addAll(pickPicBefor);
        } else if (picType == 2) {
            //修复后照片
            pickPic.addAll(pickPicAfter);
        } else if (picType == 3) {
            //照片上传
            pickPic.addAll(pickPicTakePhoto);
        }

        galleryConfig.getBuilder().isOpenCamera(false).pathList(pickPic).build();
        GalleryPick.getInstance().setGalleryConfig(galleryConfig).open((Activity) mContext);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_pic_take:
                break;
            default:
                break;
        }
    }

    public void setPICURE_NUM(int PICURE_NUM) {
        this.PICURE_NUM = PICURE_NUM;
        galleryConfig.getBuilder().multiSelect(true, PICURE_NUM);
    }

    /**
     * 显示拍照上传布局
     */
    public void showTakePhotoLayout() {
        llPicTakePhtot.setVisibility(View.VISIBLE);
        llPicRepairPhoto.setVisibility(View.GONE);
    }

    /**
     * 显示修复前和修复后布局
     */
    public void showRepairPhotoLayout() {
        llPicRepairPhoto.setVisibility(View.VISIBLE);
        llPicTakePhtot.setVisibility(View.GONE);
    }

    public LinearLayout getLlPicTakePhtot() {
        return llPicTakePhtot;
    }

    public void setLlPicTakePhtot(LinearLayout llPicTakePhtot) {
        this.llPicTakePhtot = llPicTakePhtot;
    }

    public ArrayList<String> getPickPic() {
        return pickPic;
    }

    public void setPickPic(ArrayList<String> pickPic) {
        this.pickPic = pickPic;
    }

    public ArrayList<String> getPickPicBefor() {
        return pickPicBefor;
    }

    public void setPickPicBefor(ArrayList<String> pickPicBefor) {
        this.pickPicBefor = pickPicBefor;
    }

    public ArrayList<String> getPickPicAfter() {
        return pickPicAfter;
    }

    public void setPickPicAfter(ArrayList<String> pickPicAfter) {
        this.pickPicAfter = pickPicAfter;
    }

    public GalleryConfig getGalleryConfig() {
        return galleryConfig;
    }

    public void setGalleryConfig(GalleryConfig galleryConfig) {
        this.galleryConfig = galleryConfig;
    }

    public GlideSimpleAdapter getSimpleAdapter() {
        return simpleAdapter;
    }

    public void setSimpleAdapter(GlideSimpleAdapter simpleAdapter) {
        this.simpleAdapter = simpleAdapter;
    }

    public List<HashMap<String, Object>> getDataList() {
        return dataList;
    }

    public void setDataList(List<HashMap<String, Object>> dataList) {
        this.dataList = dataList;
    }

    public int getPERMISSIONS_REQUEST_READ_CONTACTS() {
        return PERMISSIONS_REQUEST_READ_CONTACTS;
    }

    public int getPICURE_NUM() {
        return PICURE_NUM;
    }

    public boolean isGalleryConfigPick() {
        return isGalleryConfigPick;
    }

    public void setGalleryConfigPick(boolean galleryConfigPick) {
        isGalleryConfigPick = galleryConfigPick;
    }

    public IHandlerCallBack getiHandlerCallBack() {
        return iHandlerCallBack;
    }

    public void setiHandlerCallBack(IHandlerCallBack iHandlerCallBack) {
        this.iHandlerCallBack = iHandlerCallBack;
    }

    public ImageView getIvPicTake() {
        return ivPicTake;
    }

    public void setIvPicTake(ImageView ivPicTake) {
        this.ivPicTake = ivPicTake;
    }

    public ImageView getIvPicTake2() {
        return ivPicTake2;
    }

    public void setIvPicTake2(ImageView ivPicTake2) {
        this.ivPicTake2 = ivPicTake2;
    }

    public int getPicType() {
        return picType;
    }

    public void setPicType(int picType) {
        this.picType = picType;
    }

    public RelativeLayout getRlImg() {
        return rlImg;
    }

    public void setRlImg(RelativeLayout rlImg) {
        this.rlImg = rlImg;
    }

    public boolean isCanTakePhoto2() {
        return canTakePhoto2;
    }

    public void setCanTakePhoto2(boolean canTakePhoto2) {
        this.canTakePhoto2 = canTakePhoto2;
    }

    public boolean isCanTakePhoto1() {
        return canTakePhoto1;
    }

    public void setCanTakePhoto1(boolean canTakePhoto1) {
        this.canTakePhoto1 = canTakePhoto1;
    }

    public boolean isCanTakePhoto3() {
        return canTakePhoto3;
    }

    public void setCanTakePhoto3(boolean canTakePhoto3) {
        this.canTakePhoto3 = canTakePhoto3;
    }

    public SmartHeightGridView getmGridView() {
        return mGridView;
    }

    public void setmGridView(SmartHeightGridView mGridView) {
        this.mGridView = mGridView;
    }

    public SmartHeightGridView getmGridView2() {
        return mGridView2;
    }

    public void setmGridView2(SmartHeightGridView mGridView2) {
        this.mGridView2 = mGridView2;
    }

    public SmartHeightGridView getmTakePhotoGridView() {
        return mTakePhotoGridView;
    }

    public void setmTakePhotoGridView(SmartHeightGridView mTakePhotoGridView) {
        this.mTakePhotoGridView = mTakePhotoGridView;
    }

    public ImageView getIvPicTake0() {
        return ivPicTake0;
    }

    public void setIvPicTake0(ImageView ivPicTake0) {
        this.ivPicTake0 = ivPicTake0;
    }

    public ArrayList<String> getPickPicTakePhoto() {
        return pickPicTakePhoto;
    }

    public void setPickPicTakePhoto(ArrayList<String> pickPicTakePhoto) {
        this.pickPicTakePhoto = pickPicTakePhoto;
    }

    public LinearLayout getLlIsRepairAfter() {
        return llIsRepairAfter;
    }

    public void setLlIsRepairAfter(LinearLayout llIsRepairAfter) {
        this.llIsRepairAfter = llIsRepairAfter;
    }
}
